package com.appkubes.mall.restful.application.plugin.delivery.event.impl;

import com.appkubes.cloud.config.mybatisplus.MultiTenantResolver;
import com.appkubes.mall.core.entity.MallOrder;
import com.appkubes.mall.restful.application.order.order.service.OrderService;
import com.appkubes.mall.restful.application.plugin.delivery.event.DeliveryEventHandle;
import com.appkubes.mall.restful.application.plugin.delivery.service.PluginDeliveryService;
import com.appkubes.mall.restful.event.order.OrderAddEvent;
import com.appkubes.mall.restful.event.order.entity.OrderAddEventEO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * @Author: Rongjie
 * @Date: 2020/3/6
 * @Description:
 */
@Repository
@Slf4j
public class DeliveryEventHandleImpl implements DeliveryEventHandle {
    @Resource
    PluginDeliveryService pluginDeliveryService;
    @Resource
    OrderService orderService;
    @Resource
    MultiTenantResolver tenantResolver;

    @EventListener
    public void orderAdd(OrderAddEvent event) {
        //订单添加事件处理
        String tenantId = tenantResolver.getTenantId();
        if (tenantId.equalsIgnoreCase("delivery")) {
            log.info("delivery event:{}", event);
            try {
                OrderAddEventEO orderAddEventEO = event.getOrderAddEventEO();
                MallOrder order = orderService.getById(orderAddEventEO.getOrderKey());
                pluginDeliveryService.notifyOrder(order);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
